package team.fourth.combat.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.fourth.combat.commen.entity.PageResult;
import team.fourth.combat.commen.entity.QueryPageBean;
import team.fourth.combat.dao.CheckGroupMapper;
import team.fourth.combat.dao.CheckItemMapper;
import team.fourth.combat.pojo.CheckGroup;
import team.fourth.combat.pojo.CheckItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    CheckGroupMapper checkGroupMapper;

    @Autowired
    CheckItemMapper checkItemMapper;
    /**
     * 健壮性
     * @param checkGroup
     */
    @Override
    public void add(CheckGroup checkGroup) {
//        1、先插入检查组基本信息并(返回主键)
        checkGroupMapper.add(checkGroup);

        Integer checkGroupId = checkGroup.getId();//5
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();//[1,2,3,4]
        setCheckGroupAndCheckItemBatch(checkitemIds,checkGroupId);
        /**
//        2、循环用户勾选的检查项id集合和检查组id建立关系
        if(CollectionUtil.isNotEmpty(checkitemIds)){
            List<Map> batchParams = new ArrayList<>();//[{checkGroupId:5,checkitemId:6},{checkGroupId:5,checkitemId:7}]
            for (Integer checkitemId : checkitemIds) {
//                insert into 表名（checkGroupId，checkitemId）values(#{checkGroupId},#{checkitemId})

                Map map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                batchParams.add(map);
//                checkGroupMapper.setCheckGroupAndCheckItem(checkGroupId,checkitemId);
            }
            checkGroupMapper.setCheckGroupAndCheckItemBatch(batchParams);
        }
*/
    }

    public void setCheckGroupAndCheckItemBatch(List<Integer> checkitemIds,Integer checkGroupId){
        if(CollectionUtil.isNotEmpty(checkitemIds)){
            List<Map> batchParams = new ArrayList<>();
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                batchParams.add(map);
            }
            checkGroupMapper.setCheckGroupAndCheckItemBatch(batchParams);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<CheckGroup> checkGroups = checkGroupMapper.findPage(queryPageBean.getQueryString());
        return PageResult.OK(page.getTotal(),checkGroups);
    }

    @Override
    public Map findById(Integer id) {
        //根据id查询检查组详情
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        // 所有的检查项`
        List<CheckItem> checkItems = checkItemMapper.findAll();
        // `上一次用户勾的检查项id集合
        List<Integer> checkItemIds =  checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
        Map map = new HashMap();
        map.put("checkGroup",checkGroup);
        map.put("checkItems",checkItems);
        map.put("checkItemIds",checkItemIds);
        return map;
    }

    @Override
    public void edit(CheckGroup checkGroup) {
//        1、更新检查组基本信息
        checkGroupMapper.edit(checkGroup);
//        2、根据检查组id删除老的关系
        checkGroupMapper.deleteAssociation(checkGroup.getId());
//        3、重新建立关系
        setCheckGroupAndCheckItemBatch(checkGroup.getCheckitemIds(),checkGroup.getId());
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }
}
